package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.daos.MemberMapper;
import com.project.daos.zt_Order1Mapper;
import com.project.daos.zt_StallMapper;
import com.project.entity.Member;
import com.project.entity.Order1;
import com.project.entity.Stall;
import com.project.entity.Vehicle;
import com.project.service.TenantOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TenantOrderServiceImpl implements TenantOrderService {
    @Autowired
    private zt_Order1Mapper zt_order1Mapper;
    @Autowired
    private zt_StallMapper zt_stallMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public PageInfo<Order1> zt_selOrderAll_Date(Integer currentPage, Integer pageSize, Date dt1, Date dt2, String vehicle_code, int user_id) {
//        Integer vehicle_id=null;
//        if(vehicle_code!=null&&vehicle_code!=""){//必须加不等于空！！！
//            vehicle_id = zt_order1Mapper.selOrderAll_vehicle_idByCode(vehicle_code);
//            System.out.println(vehicle_id);
//        }
        PageHelper.startPage(currentPage,pageSize);
        List<Order1> order1s = zt_order1Mapper.selOrderAll_Date(dt1,dt2,vehicle_code,user_id);
        PageInfo<Order1> pageInfo = new PageInfo(order1s);
        return pageInfo;
    }

    @Override
    public PageInfo<Order1> zt_selOrder_conduct(Integer currentPage, Integer pageSize ,int user_id) {
        PageHelper.startPage(currentPage,pageSize);
        List<Order1> order1s = zt_order1Mapper.selOrder_conduct(user_id);
        PageInfo<Order1> pageInfo = new PageInfo(order1s);
        return pageInfo;
    }

    @Override
    public int zt_DoOrder1End(Date order_endtime, int order_id,int stall_id,double resTime,Integer order_user_id) {
        Member stallMember = zt_stallMapper.selBalance(stall_id);
        Double stallBalance = stallMember.getBalance();//出租方余额
        Member orderMember = zt_stallMapper.selBalance_order(order_user_id);
        Double balance_order = orderMember.getBalance();//租方余额
        int stallUserid = memberMapper.selStall_Userid(stall_id);//查询出租方id
        Stall stall = zt_stallMapper.selStallById(stall_id);
        Double stall_price = stall.getStall_price();//查询车位费用
        double money = resTime*stall_price;//费用
        int i1 = zt_order1Mapper.updateOrder1End(order_endtime, money, order_id);//订单插入结束时间，计算费用
        int i2 = zt_stallMapper.updateStall_state_End(stall_id);//修改车位状态
        int i3 = 0;
        int i4 = 0;
        if(balance_order >= money ){
            double addMoney = stallBalance + money;
            double reduceMoney = balance_order - money;
            i3 = memberMapper.addMoney(stallUserid,addMoney);//出租方加钱
            i4 = memberMapper.reduceMoney(order_user_id,reduceMoney);//租方扣钱
        }else{
            throw new RuntimeException("余额不足");
        }
        int i = i1+i2+i3+i4;
        return i;
    }


}
